package com.tank.jdk8.stream2;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/4/9 0009
 * Time: 下午 10:09
 * To change this template use File | Settings | File Templates.
 */
@Slf4j
public class StreamTest1 {


    /**
     * 1:第一个handler异常会抛给调用者，其他异常会被压制，因为后续的调用非调用者直接调用，且每个handler之间相互独立
     *2:如果 handler事件抛出的是同一个异常对象，则不会打印压制信息，因为同对象不能压制
     */
    @Test
    public void testClose(){
        List<String> list = Arrays.asList("aaa", "bbb", "ccc");
        NullPointerException nullPointerException = new NullPointerException("my exception");
        try(Stream<String> stream = list.stream()){
            stream.onClose(() -> {
                log.info("first handler invoked");
                //throw new NullPointerException("first exception");
                throw nullPointerException;
            }).onClose(() -> {
                log.info("second handler invoked");
               // throw new ClassCastException("second exception");
                throw nullPointerException;
            }).forEach(System.out::println);

        }
    }
}
